
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var helperOpen = require("helper/open");
var http = require("helper/http");
var mallUrl = require("url/mall");

var listview = ui("listView");
var listdata = mm("do_ListData");
listview.bindItems(listdata);//绑定item的数据

var noData = [{"template":1,"labelContent":"没有更多规格"}];
var addShare = ui("$").add("share","source://view/reuse/share.ui", 0, 0);
//当前页码
var pageNum = 1;
var speLst = [];
var productId = null;

/* 头部 */
var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
var labelTitle = ui(addheader+".labelTitle");
labelTitle.text='商品规格信息';

headerLeft.on("touch", function() {
	app.closePage({mall_edit_productSpec: true});
});

//andriod返回键
page.on("back", function() {
	app.closePage({mall_edit_productSpec: true});
});

page.on("loaded", function(){
	productId = page.getData().productId;
	specLst = page.getData().productSpecLst;
	loadFirstPage();
});

page.on("result", function(data){
	if(data && data.mall_edit_productSpec){
		loadSpec();
	}
});

page.on("mall_product_spec_refresh", function(data, e) {
	loadSpec();
});

//加载规格
function loadFirstPage() {
	if(listdata.getCount()>0){
		listdata.removeAll();
	}
	if(specLst.length){
		listdata.addData(specLst);//加载规格
	}else{
		listdata.addData(noData);//没有商品
	}
	listview.refreshItems();
}

//加载
function loadSpec(){
	pageNum=1;
	http.post(mallUrl.url.product.getProductSpecLst, {productId: productId}, function(data) {
		var productSpecList = data.productSpecList;
		if(productSpecList){
			if(listdata.getCount()>0){
				listdata.removeAll();
			}
			if(productSpecList.length){
				listdata.addData(productSpecList);//加载商品规格烈列
			}else{
				listdata.addData(noData);//没有商品规格
			}
			listview.refreshItems();
		}		
	});
}

//下拉列表，刷新数据
listview.on("pull", function(data){
	if (data.state == 2){
		loadSpec();
		listview.rebound();//复位【一定要做】
	}
	  
});

//上拉列表，翻页数据
listview.on("push", function(data){
	if (data.state == 2){
		listview.rebound();//复位【一定要做】
	}
});